package cn.com.surker.workcon.service.siwei;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.control.entity.ControlInvoice;
import cn.com.surker.enums.production.invoice.ControlProStatusEnum;
import cn.com.surker.production.entity.ProductionIndentDispatch;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.ProductService;
import cn.com.surker.workcon.service.bcs.common.ProductFlagEnum;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 上海思维工控机的生产数据 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class ProductServiceImpl extends GKJDBCService implements ProductService {

	/**
	 * 根据erp的派单信息,查询派车数据
	 *
	 * @param dispatchId erp的派单id
	 * @return
	 */
	public ProductionIndentDispatch queryByDispatchId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	TJlb为思维的发货单表
		String selectSql = "SELECT FNo,FZt,FScrq,FCcsj,FBcfs,FRwdh FROM TJlb where FPcbID = " + dispatchId;
		List<Map<String, Object>> maps = lineJdbcTemp.queryForList(selectSql);
		if (maps == null || maps.isEmpty()) {
			return null;
		}
		Map<String, Object> map = maps.get(0);
		//	生产时间
		////	生产日期
		String fscrq = String.valueOf(map.get("FScrq"));
		fscrq = fscrq.replaceAll("\\s+", "");
		////	出场时间
		String fccsj = String.valueOf(map.get("FCcsj"));
		String prodTimE = fscrq + " " + fccsj + ":00";
		Date prodTim = DateUtil.convertDate(prodTimE);
		//	本车方量
		Double prodMete = Double.valueOf(String.valueOf(map.get("FBcfs")));
		//	工控机id
		String prodId = String.valueOf(map.get("FNo"));
		//	工控机状态
		Integer controlProStatus = null;
		String flag = String.valueOf(map.get("FZt"));
		if ("自动".equals(flag)) {
			//	自动完成,处理发货单自动完成的情况
			controlProStatus = ControlProStatusEnum.AUTOMATIC.getCode();
		} else if ("作废".equals(flag)) {
			//	手动生产,处理发货单手动生产的情况
			controlProStatus = ControlProStatusEnum.INTERRUPT.getCode();
		} else {
			controlProStatus = ControlProStatusEnum.OTHER.getCode();
		}
		ProductionIndentDispatch pid = new ProductionIndentDispatch();
		pid.setProTime(prodTim);
		pid.setProStere(prodMete);
		pid.setControlId(prodId);
		pid.setControlProStatus(controlProStatus);
		// pid.setRecipe(recipe);
		return pid;
	}

	@Override
	public Boolean inProing(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询sql
		// String querySql = "select ID from Producing where ErpID = " + dispatchId + " and Station = " + line.getControlLineId();
		String selectSql = "select top 1 FNo,FZt from TJlb where FPcbID = ?";
		//	//查询数据
		List<Map<String, Object>> maps = lineJdbcTemp.queryForList(selectSql);
		if (maps == null || maps.isEmpty()) {
			//	没有数据
			return false;
		}
		return true;
	}

	@Override
	public List<ControlInvoice> findFullByMin(Long lineId, String lineName, Date minDate, Long minProdId) {
		if (lineId == null || minDate == null || minProdId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询数据
		/*String querySql = "select ProjName conName,Customer clientName,ProjAdr address,ConsPos site,BetLev strengthName,Lands slumps,pour castModel,MorMete mortarStere,ProdMete concreteStere,Vehicle carCode,TotVehs totalCar,operator mixer,\n" +
			"prodTimE proTime,ProdID controlNo,CASE WHEN erpid = 0 THEN 1 ELSE 0 END conAuto,? lineId,? lineName\n" +
			"from BS23DB2.dbo.Product where station = " + line.getControlLineId() + "\n";
		if (minDate != null) {
			querySql += "and prodTimE > '" + DateUtil.convertString(minDate, "yyyy-MM-dd HH:mm:ss") + "'";
		}*/
		String querySql = "select fno prodId,FGcmc conName,FHtdw clientName,fgcdz address,FJzbw site,FTpz strengthName,FTld slumps,FJzfs castModel,0 mortarStere,FBcfs concreteStere,FShch carCode,FLjcs totalCar,FCzy mixer,\n" +
			"(FScrq + ' ' + FCcsj + ':00') proTime,FNo controlNo,CASE WHEN FPcbID is null THEN 0 ELSE 1 END conAuto,? lineId,? lineName FROM TJlb ";
		if (minProdId != null) {
			querySql += "where FNo > " + minProdId + "";
		} else if (minDate != null) {
			querySql += "where (FScrq + ' ' + FCcsj + ':00') > " + DateUtil.convertString(minDate, "yyyy-MM-dd HH:mm:ss") + "'";
		}

		Object[] params = {lineId, lineName};
		List<ControlInvoice> weightSpotChecks = lineJdbcTemp.query(querySql, params, BeanPropertyRowMapper.newInstance(ControlInvoice.class));
		return weightSpotChecks;
	}

	@Override
	public List<ControlInvoice> findByTime(Long lineId, String lineName, String startTime, String endTime) {
		return null;
	}
}
